using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using ProjectEuler.Core.Helpers;

namespace ProjectEuler.Core
{
    public class Problem4: IProjectEulerProblem
    {
        // A palindromic number reads the same both ways. The largest palindrome made 
        // from the product of two 2-digit numbers is 9009 = 91  99.

        //Find the largest palindrome made from the product of two 3-digit numbers.
        public bool IsNumberAPalindrome(int n)
        {
            return n.ToString().IsPalindrome();
        }

        public IList<int> PalindromesFromProductOfAll3DigitNumbers()
        {
            var palindromes = new List<int>();
            for (int i = 100; i <= 999; i++)
            {
                for (int j = 100; j <= 999; j++)
                {
                    var test = i * j;
                    if (IsNumberAPalindrome(test))
                    {
                        palindromes.Add(test);
                    }
                }
            }
            return palindromes;
        }

        public int Number
        {
            get { return 4; }
        }

        public string Description
        {
            get { return "Find the largest palindrome made from the product of two 3-digit numbers."; }
        }

        public string Answer
        {
            get { return PalindromesFromProductOfAll3DigitNumbers().Max().ToString(); }
        }
    }

    [TestFixture]
    public class When_using_IsNumberAPalindrome
    {
        // these tests are to avoid off-by-one problems

        [Test]
        public void An_even_length_palindrome_should_pass()
        {
            const int evenlengthpalindrome = 1001;
            var prob = new Problem4();
            Assert.AreEqual(true, prob.IsNumberAPalindrome(evenlengthpalindrome));
        }

        [Test]
        public void An_odd_length_palindrome_should_pass()
        {
            const int oddlengthpalindrom = 10501;
            var prob = new Problem4();
            Assert.AreEqual(true, prob.IsNumberAPalindrome(oddlengthpalindrom));
        }
    
    }
}